package com.zsxb.crm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zsxb.crm.dto.CourseDTO;
import com.zsxb.crm.dto.CoursePageDTO;
import com.zsxb.crm.mapper.CourseMapper;
import com.zsxb.crm.pojo.Course;
import com.zsxb.crm.result.PageResult;
import com.zsxb.crm.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    /**
     * 课程分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult pageQuery(CoursePageDTO dto) {
//        if (dto.getParams()!=null){
//            String beginTime = dto.getParams().get("beginCreateTime").toString();
//            dto.setBeginCreateTime(beginTime);
//            String endTime =  dto.getParams().get("endCreateTime").toString();
//            dto.setEndCreateTime(endTime);
//        }
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        log.info("课程分页查询参数:{}",dto);
        Page<Course> page = courseMapper.pageQuery(dto);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRows(page.getResult());
        return pageResult;
    }

    /**
     * 根据id查询课程信息
     * @param id
     * @return
     */
    @Override
    public Course getById(Long id) {
        log.info("根据id查询课程信息:{}",id);
        Course course = courseMapper.getById(id);
        return course;
    }

    /**
     * 修改课程信息
     * @param course
     */
    @Override
    public void update(Course course) {
        log.info("修改课程信息:{}",course);
        courseMapper.update(course);
    }

    /**
     * 批量删除课程信息
     * @param ids
     */
    @Override
    public void delete(String ids) {
        log.info("批量删除课程信息:{}",ids);
        String[] split = ids.split(",");
        courseMapper.delete(split);
    }

    /**
     * 添加课程信息
     * @param dto
     */
    @Override
    public void add(CourseDTO dto) {
        Course course = new Course();
        BeanUtils.copyProperties(dto,course);
        log.info("添加课程信息:{}",course);
//        course.setCode(UUID.randomUUID().toString());
        courseMapper.add(course);
    }



    /**
     * 合同处查询课程
     */
    @Override
    public List<Course> listselect(CourseDTO dto) {

        return courseMapper.listselect(dto);
    }
}
